import {store} from "@/js/store";
import {readRoom, roomJson} from "@/js/util/room";
import {parser} from "xijs";

export const onOpen=(data)=>{

}
export const onMessage=(data)=>{
    let message=parser.parse(data.data)
    console.log(message)
    switch (message['type']){
        case 'message':{
            store.commit('pushMessage',{date:message.date,text:message.text})
            return
        }
        case 'init_room':{
            if (message['init_user']+''==='$self'||message['init_user']+''===store.state.name+''){
                readRoom(message['data'])
            }else {
                let newMessage=parser.stringify({
                    type:'init_room',
                    data:roomJson(),
                    init_user:message['init_user']+''
                })
                store.commit("send",newMessage)
            }
            break
        }
        case 'card_add':{
            store.commit("pushCardList",message.data.card)
            break
        }
        case 'card_update':{
            store.commit("updateCard",{index:message.data.index,card:message.data.card})
            break
        }
        case 'card_delete':{
            store.commit("deleteCardList",message.data.index)
            break
        }
        default:
            break;
    }
}
export const onClose=(data)=>{

}
